package com.yb.guigu.pick.security.bean;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/31 15:56
 * FileName: GuiguPickUser
 * Description:
 */

@Data
public class GuiguPickUser implements UserDetails {


    @ApiModelProperty("主键")
    private Integer id;

    @ApiModelProperty("系统分配")
    private Integer userId;

    @ApiModelProperty(" 用户名")
    private String username;


    @ApiModelProperty(" 真实姓名")
    private String realname;

    @ApiModelProperty("密码")
    private String password;

    @ApiModelProperty("加密方式 1 Bcrpt")
    private String encryptedType;

    @ApiModelProperty(" email")
    private String email;


    @ApiModelProperty(" phone")
    private String phone;

    @ApiModelProperty(" avatar")
    private String avatar;


    @ApiModelProperty("是否启用 0 未启用 1 启用")
    private Integer enabled;

    @ApiModelProperty("是否过期 0 过期 1未过期")
    private Integer accountNonExpired;

    @ApiModelProperty("是否被锁定 0 被锁定 1 未被锁定")
    private Integer accountNonLocked;

    @ApiModelProperty("密码是否过期 0 过期 1未过期")
    private Integer credentialsNonExpired;

    @ApiModelProperty("创建时间")
    private LocalDateTime createTime;

    @ApiModelProperty("更新时间")
    private LocalDateTime updateTime;


    private Collection<GrantedAuthority> authorities;


    private String code;

    private String loginType;

    private String loginAccessToken;

    private LocalDateTime loginTime;


    public GuiguPickUser(){

    }


    public GuiguPickUser(GuiguPickUserRequest guiguPickUserRequest){
            this.username=guiguPickUserRequest.getUsername();
            this.password= guiguPickUserRequest.getPassword();
            this.code=guiguPickUserRequest.getCode();
            this.loginType=guiguPickUserRequest.getLoginType();
            this.email=guiguPickUserRequest.getEmail();
            this.phone=guiguPickUserRequest.getPhone();
    }

    public void setAuthorities(List<GuiguPickGrantedAuthority> authorities) {

        if(CollectionUtils.isEmpty(authorities))return;

        if(this.authorities==null){
            this.authorities=new ArrayList<>();
        }

       this.authorities = Collections.unmodifiableList(new ArrayList<>(authorities));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired==1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked==1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired==1;
    }

    @Override
    public boolean isEnabled() {
        return enabled==1;
    }
}
